//
//  XQCourseDetailViewController.swift
//  XiaoQiQiaoSwift
//
//  Created by tech-lab on 2016/11/29.
//  Copyright © 2016年 校企桥. All rights reserved.
//
import Alamofire
import ObjectMapper
import AlamofireObjectMapper
import SwiftyJSON
import UIKit

class XQCourseDetailViewController: XQBaseViewController,UITableViewDelegate,UITableViewDataSource {

    var cid = String()
    var type = String()
    private var tableHeaderView = XQCourseDetailTableHeaderView()
    
    private var courseDetailMode:XQCourseDetailMode?
    
    private  var tableView = UITableView()
    private let XQHomeFourTableViewCellID = "XQHomeFourTableViewCellID"
    private let XQCourseDetailFirstTableViewCellID = "XQCourseDetailFirstTableViewCellID"
    
    override func viewWillAppear(_ animated: Bool) {
    self.navigationController?.isNavigationBarHidden = true
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        self.navigationController?.isNavigationBarHidden = false

    }

    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */
    

    
    
    override func setRootView() {
      
        tableHeaderView = XQCourseDetailTableHeaderView.init(frame: CGRect.init(x: 0, y: 0, width: SCREEN_WIDTH, height: 120))
        tableHeaderView.homeself = self
        
        tableHeaderView.backButton.addTarget(self, action: #selector(self.backLeft), for: .touchUpInside)
        
        self.tableView = UITableView.init(frame:  CGRect.zero, style: .grouped)
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        self.tableView.showsVerticalScrollIndicator = false
        self.view.addSubview(self.tableView)
        
        tableView.snp.makeConstraints { (make) in
            make.left.equalTo(self.view.snp.left)
            make.right.equalTo(self.view.snp.right)
            make.top.equalTo(self.view.snp.top).offset(20)
            make.bottom.equalTo(self.view.snp.bottom)
        }
        
        self.tableView.tableHeaderView = tableHeaderView

        self.tableView .register(XQHomeFourTableViewCell.self, forCellReuseIdentifier: XQHomeFourTableViewCellID)
        self.tableView .register(XQCourseDetailFirstTableViewCell.self, forCellReuseIdentifier: XQCourseDetailFirstTableViewCellID)
        
        courseContentData()
    }
    
    private func courseContentData() {
        
        let urlstr = MTBaseURL.appending("NewVerCourse/courseIntro.json")

        let para: Parameters = ["uid":"","cid":cid,"type":type]

        Alamofire.request(urlstr, parameters:para).responseObject { (response:DataResponse<XQCourseDetailMode>) in
           
            if response.result.isFailure {
                print("Error in request")
                return
            }
            self.courseDetailMode = response.result.value
            
            self.tableHeaderView.csMode = self.courseDetailMode?.cs
            
            self.tableView.reloadData()
        }

    
    }
    
    
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 2
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if section == 0 {
            return 0.1
        }else{
            return 30
        }
    }
    
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        if section == 1 {
            return "相关职位"
        }else{
            return ""
        }
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.1
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.section == 0 {
            return 315
        }else{
            return 120
        }
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        if indexPath.section == 0 {
            let firstCell = tableView.dequeueReusableCell(withIdentifier: XQCourseDetailFirstTableViewCellID) as!XQCourseDetailFirstTableViewCell
            if ((self.courseDetailMode?.cs) != nil) {
                firstCell.csMode = self.courseDetailMode?.cs
            }
            if ((self.courseDetailMode?.ms) != nil) {
                firstCell.msMode = self.courseDetailMode?.ms
            }
            return firstCell
            
        }else{
            let relateCell = tableView.dequeueReusableCell(withIdentifier: XQHomeFourTableViewCellID) as!XQHomeFourTableViewCell
            if ((self.courseDetailMode?.ps) != nil) {
                relateCell.dataArray = (self.courseDetailMode?.ps)!
                relateCell.collectionView.reloadData()
            }
            return relateCell
        }

    }
}
